package com.yejhxt.controllerImpl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import com.yejhxt.apiInterface.ProfileInterface;
import com.yejhxt.inter.roleMapper;
import com.yejhxt.model.role;
import com.yejhxt.model.user;
import com.yejhxt.tools.ProjectTools;

import sun.misc.BASE64Decoder;


public class ProfileController  {
	private roleMapper roleMapper;
	
	private SimpleDateFormat sdf;



	public ProfileController(com.yejhxt.inter.roleMapper roleMapper) {
		super();
		this.roleMapper=roleMapper;
		sdf = new SimpleDateFormat("yyyy-MM-dd");
	}

	
	public role ProfileSelectHandler(HttpSession session) {
		role role = null;
		// System.out.println(session+" inin ");
		if (session != null) {
			user user = (user) session.getAttribute("User");
			if (user != null) {
				System.out.println(user.getUserid());
				role = roleMapper.selectByPrimaryKey(user.getUserid());
			}
		}
		return role;
	}

	public boolean canUpdate(String realname,String email, String address,String  phone,String qq) {
	   if(StringUtils.isEmpty(realname)||StringUtils.isEmpty(email)||StringUtils.isEmpty(address)||StringUtils.isEmpty(phone)
			  ||StringUtils.isEmpty(qq))
	   {
		 
		   return false;
	   }
		return true;
	}






	

	
	public void ProfileSelectHandler(String sid, HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		// TODO Auto-generated method stub
		
	}

	public int ProfileUpdateHandler(String sid, String realname, String gender, String occupation, String company,
			String address, String phone, String email, String qq, HttpServletRequest request,
			HttpServletResponse response, Model model, HttpSession sess) {
		if (sess != null) {
			user user = (user) sess.getAttribute("User");
			if (user != null) {// login
				
				if (canUpdate(realname, email, address, phone,qq)) {// 必填项完整且正确
					role role = roleMapper.selectByPrimaryKey(user.getUserid());
					// 必填
					role.setRealname(realname);
					role.setGender(gender);
					 role.setAddress(address);
					 role.setPhone(phone);
						role.setModifytime(new Date());
					role.setOccupation(occupation);
					role.setCompany(company);
					if (!StringUtils.isEmpty(qq)) {
						String pattern = "[1-9]([0-9]{5,11})";
						Pattern r = Pattern.compile(pattern);
						Matcher m = r.matcher(qq);
						if (m.matches())
							role.setQq(qq);
						else
							return 406;
					}
					
					if (!StringUtils.isEmpty(email)) {
						String pattern = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
						Pattern r = Pattern.compile(pattern);
						Matcher m = r.matcher(email);
						if (m.matches())
							role.setEmail(email);
						else
							return 406;
					}
					role.setModifytime(new Date());

					roleMapper.updateByPrimaryKeySelective(role);
					sess.setAttribute("Role", role);
					return 200;
				}
				return 406;
			}
		}
		return 403;

		
	}
}